import java.util.Arrays;

public class MyArrayList implements IList{
    public int[] elem;
    public int usedSize;

    public static final int DEF_CAPACITY = 5;

    public MyArrayList(){
        elem = new int[DEF_CAPACITY];
    }

    public boolean isFull(){
        return this.elem.length == this.usedSize;
    }

    public void grow(){
        elem = Arrays.copyOf(elem, elem.length*2);
    }

    @Override
    public void add(int data) {
        // 1. 检查是否满了, 满了则扩容
        if(isFull()){
            grow();
        }
        // 2. 添加数据
        elem[usedSize] = data;
        usedSize++;
    }

    @Override
    public void add(int pos, int data) {
        // 1. 检查pos位置是否在访问内
        String msg = "add方法中pos的位置不合法";
        checkAddPos(pos, msg);

        // 2. 检查是否满了
        if(isFull()){
            grow();
        }

        // 3. 检查是否为尾插
        if(pos == usedSize){
            elem[pos] = data;
            usedSize++;
            return;
        }

        // 4. 移动元素
        for (int i = this.usedSize - 1; i >= pos ; i--) {
            elem[i+1] = elem[i];
        }

        // 5. 添加数据
        elem[pos] = data;
        usedSize++;

    }

    private void checkAddPos(int pos, String msg) {
        if(pos < 0 || pos > this.usedSize){
            throw new ErrorPosException(msg);
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        // 1. 检查下标是否为空
        if(emptyList()){
            throw new EmptyListException("顺序表为空!!!");
        }

        // 2. 检查下标
        String msg = "Get方法中pos的位置不合法";;
        checkPos(pos, msg);

        // 3. 获取元素
        return elem[pos];
    }

    private boolean emptyList() {
        return usedSize == 0;
    }


    private void checkPos(int pos, String msg) {
        if(pos < 0 || pos >= usedSize){
            throw new ErrorPosException(msg);
        }
    }

    @Override
    public void set(int pos, int value) {
        // 1. 检查下标是否为空
        if(emptyList()){
            throw new EmptyListException("顺序表为空, 不能获取元素");
        }

        // 2. 检查下标
        String msg = "Set方法中pos的位置不合法";;
        checkPos(pos, msg);

        // 3. 设置元素
        elem[pos] = value;

    }



    @Override
    public void remove(int toRemove) {
        // 1. 顺序表为空不能删除
        if(emptyList()){
            throw new EmptyListException("顺序表为空, 不能删除元素");
        }

        // 2. 找到你要删除的下标
        int index = indexOf(toRemove);
        if(index == -1){
            System.out.println("没有你要删除的元素");
            return;
        }

        // 3. 删除元素
        for (int i = index; i < usedSize - 1; i++) {
            elem[i] = elem[i+1];
        }

        usedSize--;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < usedSize; i++) {
            elem[i] = 0;
            // elem[i] = null;
        }
        usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }
}
